package com.mkl.ttxt.remote.controller;

import com.mkl.ttxt.common.bo.PageBean;
import com.mkl.ttxt.common.bo.PageResponce;
import com.mkl.ttxt.common.bo.TtxtResult;
import com.mkl.ttxt.common.constant.BaseConstant;
import com.mkl.ttxt.remote.bo.TtxtUser;
import com.mkl.ttxt.remote.service.ITtxtUserService;
import com.mkl.ttxt.remote.util.TokenUtil;
import com.mkl.ttxt.remote.util.loginUtil.AuthenticationInterceptor;
import com.mkl.ttxt.remote.util.loginUtil.UserLoginToken;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
@Api(description = "用户管理")
public class TtxtUserController {

    @Autowired
    private ITtxtUserService iTtxtUserService;

    private static final Logger logger = LoggerFactory.getLogger(TtxtUserController.class);

    @RequestMapping(value = "/login")
    @ResponseBody
    @ApiOperation(value = "单点登录", httpMethod = "POST")
    public TtxtResult getTtxtUser(@RequestBody TtxtUser ttxtUser, HttpServletResponse httpServletResponse) {
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        if (StringUtil.isNullOrEmpty(ttxtUser.getUserName()) || StringUtil.isNullOrEmpty(ttxtUser.getPassword())) {
            ttxtResult.setCode(BaseConstant.FAILED);
            ttxtResult.setMessage("用户名或密码不能为空！");
            return ttxtResult;
        }
        ttxtUser = iTtxtUserService.login(ttxtUser);
        if (ttxtUser == null) {
            ttxtResult.setCode(BaseConstant.FAILED);
            ttxtResult.setMessage("用户名或密码错误！");
            return ttxtResult;
        }
        String token = TokenUtil.getToken(ttxtUser, new Date());
        ttxtResult.setData(token);
        TokenUtil.loginRecordToken(ttxtUser.getUserId(), token);
        httpServletResponse.setHeader("token", String.valueOf(ttxtResult.getData()));
        logger.info("登陆" + ttxtUser.getUserId());
        return ttxtResult;
    }

    //修改个人密码
    @UserLoginToken
    @RequestMapping(value = "/updateUserPassword")
    @ResponseBody
    @ApiOperation(value = "修改个人密码", httpMethod = "POST")
    public TtxtResult updateUserPassword(@RequestParam String password, HttpServletRequest httpServletRequest) {
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        try {
            iTtxtUserService.updateUserPassword(userId, password);
        } catch (Exception e) {
            ttxtResult.setCode(BaseConstant.FAILED);
            ttxtResult.setMessage(e.getMessage());
            logger.error(e.getMessage());
        }
        return ttxtResult;
    }

}
